package GUILayer;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import ControlLayer.CtrSalesOrder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SalesOrderGUI extends JPanel{
	private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	private Date date = new Date();
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private CtrSalesOrder ctrSalesOrder  = new CtrSalesOrder();
	private JLabel label;
	private JLabel lblCustomerNotFound;
	private JLabel label_3;
	private JCheckBox chckbxNewCheckBox;
	private JLabel label_2;
	private JLabel label_1;
	/**
	 * Create the panel.
	 */
	public SalesOrderGUI() {
		setLayout(null);
		setBounds(0,0,240,307);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 240, 307);
		add(panel);
		panel.setLayout(null);
		
		JLabel lblSaleid = new JLabel("SaleID:");
		lblSaleid.setBounds(10, 11, 54, 14);
		panel.add(lblSaleid);
		
		label = new JLabel("");
		label.setBounds(74, 11, 142, 14);
		label.setText(Integer.toString(ctrSalesOrder.getMaxSaleID()));
		panel.add(label);
		
		JLabel lblCustomer = new JLabel("Customer:");
		lblCustomer.setBounds(10, 36, 54, 14);
		panel.add(lblCustomer);
		
		label_1 = new JLabel("");
		label_1.setBounds(74, 36, 142, 14);
		panel.add(label_1);
		
		JButton btnSetDiscounts = new JButton("Set discounts");
		btnSetDiscounts.setBounds(10, 243, 142, 23);
		panel.add(btnSetDiscounts);
		
		JButton btnConfirmSalesOrder = new JButton("Confirm sales order");
		btnConfirmSalesOrder.setBounds(10, 273, 142, 23);
		panel.add(btnConfirmSalesOrder);
		btnConfirmSalesOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
				ctrSalesOrder.confirmSalesOrder(textField_2.getText(), dateFormat.parse(label_2.getText())
						,Double.parseDouble(label_3.getText()), textField.getText(),
						dateFormat.parse(textField_1.getText()), chckbxNewCheckBox.isSelected(), Integer.parseInt(label.getText()));
				}catch(NumberFormatException e){
					e.printStackTrace();
				}catch(ParseException pe){
					pe.printStackTrace();
				}
			}
		});
		
		JLabel lblDate = new JLabel("Date:");
		lblDate.setBounds(10, 61, 46, 14);
		panel.add(lblDate);
		
		JLabel lblAmount = new JLabel("Amount:");
		lblAmount.setBounds(10, 86, 46, 14);
		panel.add(lblAmount);
		
		JLabel lblDeliveryStatus = new JLabel("Delivery status:");
		lblDeliveryStatus.setBounds(10, 111, 76, 14);
		panel.add(lblDeliveryStatus);
		
		JLabel lblNewLabel = new JLabel("Delivery date:");
		lblNewLabel.setBounds(10, 136, 76, 14);
		panel.add(lblNewLabel);
		
		JLabel lblClub = new JLabel("Club");
		lblClub.setBounds(10, 161, 54, 14);
		panel.add(lblClub);
		
		label_2 = new JLabel("");
		label_2.setBounds(74, 61, 142, 14);
		panel.add(label_2);
		label_2.setText(dateFormat.format(date));
		
		label_3 = new JLabel("");
		label_3.setBounds(74, 86, 100, 14);
		panel.add(label_3);
		
		textField = new JTextField();
		textField.setBounds(96, 108, 120, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(96, 133, 120, 20);
		textField_1.setText("year/month/day");
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		chckbxNewCheckBox = new JCheckBox("");
		chckbxNewCheckBox.setBounds(40, 157, 66, 23);
		panel.add(chckbxNewCheckBox);
		
		JButton btnNewButton = new JButton("Add customer");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					String lname = ctrSalesOrder.findCustomer(textField_2.getText()).getLname();
					if(!lname.equals(null)){
						label_1.setText(lname);
						lblCustomerNotFound.setText("Customer added");
					}
				}catch(Exception ex){
					lblCustomerNotFound.setText("Customer not found.");
				}
			}
		});
		btnNewButton.setBounds(10, 213, 142, 23);
		panel.add(btnNewButton);
		
		textField_2 = new JTextField();
		textField_2.setBounds(10, 186, 76, 20);
		panel.add(textField_2);
		textField_2.setColumns(10);
		
		lblCustomerNotFound = new JLabel("");
		lblCustomerNotFound.setBounds(96, 187, 120, 14);
		panel.add(lblCustomerNotFound);
		
		JLabel lblDkk = new JLabel("DKK");
		lblDkk.setBounds(184, 86, 46, 14);
		panel.add(lblDkk);
	}
	public void setAmount(double amount){
		label_3.setText(Double.toString(amount));
	}
}